package com.example.javamaday.leetcode;

public class LeetCodeTest0066 {

    public static void main(String[] args) {

    }

    public int[] plusOne(int[] digits) {

        boolean needPlusUp = false;
        boolean needAddOne = false;
        for (int i = digits.length - 1; i >= 0; i--) {
            if (i == digits.length - 1 || needPlusUp) {
                digits[i]++;
            }
            if (digits[i] > 9) {
                digits[i] = 0;
                needPlusUp = true;
            } else {
                needPlusUp = false;
            }
            if (i == 0 && needPlusUp) {
                needAddOne = true;
            }
        }

        if (needAddOne) {
            int[] result = new int[digits.length + 1];
            result[0] = 1;
            System.arraycopy(digits, 0, result, 1, result.length - 1);
            return result;
        }
        return digits;
    }
}
